using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace TST.SharePoint.Shared
{
    public static class FieldFactory
    {
        public static Field GetField(SPField field)
        {
            Field result = null;
            switch (field.Type)
            {
                case SPFieldType.Counter:
                    result = new WSSFieldNumber(field);
                    break;
                case SPFieldType.Number:
                    result = new WSSFieldNumber((SPFieldNumber)field);
                    break;
                case SPFieldType.Currency:
                    result = new WSSFieldCurrency((SPFieldCurrency)field);
                    break;
                case SPFieldType.Boolean:
                    result = new WSSFieldBoolean(field.Id.ToString());
                    break;
                case SPFieldType.DateTime:
                    result = new WSSFieldDateTime((SPFieldDateTime)field);
                    break;
                case SPFieldType.Lookup:
                    result = new WSSFieldLookup(field.Id.ToString());
                    break;
                case SPFieldType.Calculated:
                    result = new WSSFieldCalculated((SPFieldCalculated)field);
                    break;
                case SPFieldType.User:
                    result = new WSSFieldUser(field.Id.ToString());
                    break;
                case SPFieldType.MultiChoice:
                    result = new WSSFieldMultiChoice(field.Id.ToString());
                    break;
                case SPFieldType.URL:
                    result = new WSSFieldHyperlink(field as SPFieldUrl);
                    break;
                case SPFieldType.File:
                    result = new WSSFieldFile(field.Id.ToString());
                    break;
                case SPFieldType.Text:
                    if (field is SPFieldText)
                        result = new WSSFieldText((SPFieldText)field);
                    else
                        result = new FieldInvalid(field.Id.ToString());
                    break;
                case SPFieldType.Invalid:
                    if (string.Compare(field.TypeAsString, "Invalid", true) == 0)
                    {
                        result = new FieldInvalid(field.Id.ToString());
                        break;
                    }
                    else
                    {
                        // This is a custom field.
                        result = new WSSFieldCustom(field);
                        break;
                    }
                case SPFieldType.Note:
                    result = new FieldInvalid(field.Id.ToString());
                    break;
                default:
                    result = new Field(field.Id.ToString());
                    break;
            }
            if (result != null)
            {
                result.Id = field.InternalName;
                result.InternalId = field.Id.ToString("B");
                result.Name = field.InternalName;
                result.DisplayName = field.Title;
            }
            return result;
        }
    }
}
